import http from '../../http'
import type { ApiResponse } from '@/types/api'

export interface RoleVO {
  id: number
  code: string
  name: string
  description?: string
  dataScope?: string
  status?: number
  createTime?: string
}

export interface PermissionVO {
  id: number
  code: string
  name: string
  type?: string
  method?: string
  path?: string
  description?: string
  status?: number
  createTime?: string
}

export async function listRoles(keyword?: string) {
  const resp = await http.get<ApiResponse<RoleVO[]>>('/admin/roles', { params: { keyword } })
  return resp.data
}

export async function listPermissions(keyword?: string) {
  const resp = await http.get<ApiResponse<PermissionVO[]>>('/admin/permissions', { params: { keyword } })
  return resp.data
}

export async function getUserRoleIds(userId: number) {
  const resp = await http.get<ApiResponse<number[]>>(`/admin/users/${userId}/roles`)
  return resp.data
}

export async function setUserRoleIds(userId: number, roleIds: number[]) {
  const resp = await http.post<ApiResponse<boolean>>(`/admin/users/${userId}/roles`, roleIds)
  return resp.data
}

export interface UserBrief {
  id: number
  username: string
  nickname?: string
  email?: string
  avatar?: string
  status?: number
  createTime?: string
}

export async function pageUsers(q: string | undefined, pageNo: number, pageSize: number) {
  const resp = await http.get<ApiResponse<{ total: number; records: UserBrief[] }>>('/admin/users', { params: { q, pageNo, pageSize } })
  return resp.data
}

// Role CRUD
export async function createRole(payload: { code: string; name: string; description?: string; dataScope?: string; status?: number }) {
  const resp = await http.post<ApiResponse<any>>('/admin/roles', payload)
  return resp.data
}

export async function updateRole(id: number, payload: { code?: string; name?: string; description?: string; dataScope?: string; status?: number }) {
  const resp = await http.put<ApiResponse<boolean>>(`/admin/roles/${id}`, payload)
  return resp.data
}

export async function deleteRole(id: number) {
  const resp = await http.delete<ApiResponse<boolean>>(`/admin/roles/${id}`)
  return resp.data
}

export async function getRolePermIds(roleId: number) {
  const resp = await http.get<ApiResponse<number[]>>(`/admin/roles/${roleId}/perms`)
  return resp.data
}

export async function setRolePermIds(roleId: number, permIds: number[]) {
  const resp = await http.post<ApiResponse<boolean>>(`/admin/roles/${roleId}/perms`, permIds)
  return resp.data
}

// Permission CRUD
export async function createPermission(payload: { code: string; name: string; type?: string; method?: string; path?: string; description?: string; status?: number }) {
  const resp = await http.post<ApiResponse<any>>('/admin/permissions', payload)
  return resp.data
}

export async function updatePermission(id: number, payload: { code?: string; name?: string; type?: string; method?: string; path?: string; description?: string; status?: number }) {
  const resp = await http.put<ApiResponse<boolean>>(`/admin/permissions/${id}`, payload)
  return resp.data
}

export async function deletePermission(id: number) {
  const resp = await http.delete<ApiResponse<boolean>>(`/admin/permissions/${id}`)
  return resp.data
}

// Operation Logs
export interface OperationLogVO {
  id: number
  userId?: number
  username?: string
  operation: string
  method?: string
  params?: string
  ip?: string
  userAgent?: string
  status?: number
  errorMsg?: string
  executeTime?: number
  createTime?: string
}

export async function pageOpLogs(q: string | undefined, status: number | undefined, pageNo: number, pageSize: number) {
  const resp = await http.get<ApiResponse<{ total: number; records: OperationLogVO[] }>>('/admin/oplogs', { params: { q, status, pageNo, pageSize } })
  return resp.data
}

// System Configs
export interface SystemConfigVO { id: number; configKey: string; configValue?: string; description?: string; createTime?: string }
export async function listConfigs(prefix?: string) {
  const resp = await http.get<ApiResponse<SystemConfigVO[]>>('/admin/configs', { params: { prefix } })
  return resp.data
}
export async function upsertConfig(payload: { key: string; value: string; description?: string }) {
  const resp = await http.post<ApiResponse<boolean>>('/admin/configs', payload)
  return resp.data
}

// Cache Ops
export async function scanCacheKeys(pattern?: string, limit?: number) {
  const resp = await http.get<ApiResponse<string[]>>('/admin/cache/keys', { params: { pattern, limit } })
  return resp.data
}
export async function deleteCache(pattern?: string) {
  const resp = await http.delete<ApiResponse<number>>('/admin/cache', { params: { pattern } })
  return resp.data
}
export async function cachePing() {
  const resp = await http.get<ApiResponse<boolean>>('/admin/cache/ping')
  return resp.data
}

// Online Sessions
export interface OnlineSessionVO { token: string; userId?: number; username?: string; roles?: string; ttlSeconds?: number }
export async function listSessions(q?: string) {
  const resp = await http.get<ApiResponse<OnlineSessionVO[]>>('/admin/sessions', { params: { q } })
  return resp.data
}
export async function kickout(token: string) {
  const resp = await http.delete<ApiResponse<boolean>>(`/admin/sessions/${token}`)
  return resp.data
}


